package com.lotterymanage.common;

import com.lotterymanage.domain.SystemIP;

/**
 * 验证IP.
 * @author lxz
 * @date Apr 20, 2010
 */
public class CheckIP {

    /**
     *  验证IP.
     * @param targetIP String
     * @param systemIP String
     * @return true false
     */
    public boolean check(final String targetIP, final SystemIP systemIP) {
        boolean b = false;
        if (systemIP.getStartIP() == null || systemIP.getEndIP() == null) {
            return true;
        }

        String startIP = systemIP.getStartIP();
        String endIP = systemIP.getEndIP();

        String s3 = targetIP.substring(0, targetIP.lastIndexOf("."));
        String startIP3 = startIP.substring(0, startIP.lastIndexOf("."));
        String endIP3 = endIP.substring(0, endIP.lastIndexOf("."));

        String s4 = targetIP.substring(targetIP.lastIndexOf(".") + 1);
        String startIP4 = startIP.substring(startIP.lastIndexOf(".") + 1);
        String endIP4 = endIP.substring(endIP.lastIndexOf(".") + 1);

        int is = Integer.parseInt(s4);
        int istartIP = Integer.parseInt(startIP4);
        int iendIP = Integer.parseInt(endIP4);

        if (s3.equals(startIP3) && s3.equals(endIP3)) {
            if (is <= iendIP) {
                if (is >= istartIP) {
                    b = true;
                }
            }
        }

        return b;
    }

}
